﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using BEPUphysics.Entities;
using BEPUphysics.Entities.Prefabs;
using BEPUphysics;

namespace Brain.Bepu
{
    public class PhysicObjectEntity : IPhysicsObject3D
    {
        public Vector3 RotationRadians
        {
            get { return Vector3.Zero; }
            set { Orientation = QuaternionUtil.CreateQuaternion(value); }
        }
        /// <summary>
        /// User added to the Space, user delete from Space
        /// </summary>
        public void Dispose()
        {
        }
        public Entity Entity;

        public Vector3 Scale { get; set; }
        
        public Quaternion Orientation
        {
            get { return Entity.Orientation; }
            set { Entity.Orientation = value; }
        }

        public object Tag { get; set; }

        public Vector3 Position
        {
            get { return Entity.Position; }
            set { Entity.Position = value; }
        }

        public bool Immovable
        {
            get { return !Entity.IsDynamic; }
            set
            {
                if (value)
                {
                    Entity.BecomeKinematic();
                }
                else
                {
                    Entity.BecomeDynamic(1);
                }
            }
        }

        public float Mass
        {
            get { return Entity.Mass; }
            set { Entity.Mass = value; }
        }

        public PhysicObjectEntity(Entity en)
            : base()
        {
            this.Entity = en;

            Entity.PositionUpdated += new Action<Entity>(Box_PositionUpdated);
        }

        public void Box_PositionUpdated(Entity obj)
        {
            if (OnUpdate != null)
            {
                OnUpdate();
            }
        }

        public event SetVoid OnUpdate;


        public Vector3 LinearVelocity
        {
            get
            {
                return Entity.LinearVelocity;
            }
            set
            {
                Entity.LinearVelocity = value;
            }
        }
        public Vector3 AngularVelocity
        {
            get
            {
                return Entity.AngularVelocity;
            }
            set
            {
                Entity.AngularVelocity = value;
            }
        }

        public bool ShouldSerialize { get; set; }

        public void SerializeData(SerializationData parentData, string index)
        {
            // We do NOT need to serialize Position/Scale:
            // Position : The WorldObject parent of this PhysicsObject will save the Position
            // Scale    : The WorldObject has an Offset for setting this Scale

            parentData.AddData("PO.Rotation", this.Orientation, index);
            parentData.AddData("PO.Immovable", this.Immovable, index);
        }

        public void DeserializeData(SerializationData parentData, string index)
        {
            this.Orientation = parentData.GetData<Quaternion>("PO.Rotation", index);
            this.Immovable = parentData.GetData<bool>("PO.Immovable", index);
        }

        public void DeserializedAll()
        {
        }
    }
}
